<docs>
  ## 编辑企业信息
</docs>
<template>
  <div class="wrap" ref="wrap">
    <my-bread title="单位信息维护" :goback="true"></my-bread>
    <co-form :model="data" :rules="rules" ref="form">
      <co-input label="统一社会信用码" v-model="data.zjhm" :disabled="true"></co-input>
      <co-select prop="dwlx" label="单位类型" v-model="data.dwlx"
        :list="codeTable.zyjs_d_dwlx"></co-select>
      <co-input label="单位名称" v-model="data.dwmc" :disabled="true"></co-input>
      <co-level-picker
        label="单位行业"
        prop="dwhy"
        v-model="data.dwhy"
        :name="data.dwhy_mc"
        table-name="cdg_industry_all"
        :flag="math3"
      ></co-level-picker>
      <co-select prop="qygm" label="企业规模" v-model="data.qygm" @change="qygmClick"
        :list="codeTable.zyjs_d_qygm"></co-select>
      <co-input prop="qyrs" label="企业人数" v-model="data.qyrs"></co-input>
      <div v-if="codeTable.zyjs_d_qybq.length>0">
        <tree-select prop="qybq" label="企业标签" :place="data.qybq_dmmc" :items="codeTable.zyjs_d_qybq" v-model="data.qybq"></tree-select>
      </div>
      <!-- <co-select
        label="是否发布"
        v-model="data.sffb"
        :flag='false'
        :list="sffbList"
      ></co-select> -->
      <co-select prop="dwxz" label="单位性质" v-model="data.dwxz"
        :list="codeTable.cdl_orgtype"></co-select>
      <co-select prop="qtlx" label="其他登记注册类型" v-model="data.qtdjzclx" v-show="qtflag"
        :list="codeTable.cdl_orgtype_01"></co-select>
      <div v-if="codeTable.cdg_regtype.length>0">
        <tree-moresele prop="qydj" v-show="qyflag" :place="data.qydjzclx_mc" label="企业登记注册类型" :items="codeTable.cdg_regtype" v-model="data.qydjzclx"></tree-moresele>
      </div>
      <co-input label="单位经办人" v-model="data.dwjbr" placeholder="请输入单位经办人"></co-input>
      <co-level-picker
        label="注册地行政区划"
        prop="zcdxzqh"
        v-model="data.zcdxzqh"
        :name="data.zcdxzqh_mc"
        table-name="cdg_regioncode"
        :flag="math1"
      ></co-level-picker>
      <co-level-picker
        label="经营地行政区划"
        prop="jydxzqh"
        v-model="data.jydxzqh"
        :name="data.jydxzqh_mc"
        table-name="cdg_regioncode"
        :flag="math2"
      ></co-level-picker>
      <co-input label="经营地址" prop="jydz" v-model="data.jydz" placeholder="请输入经营地址"></co-input>
      <co-textarea prop="dwjj" label="单位简介" v-model="data.dwjj"></co-textarea>
      <co-input label="联系人电话" prop="lxrdh" v-model="data.lxrdh" placeholder="请输入联系人电话"></co-input>
      <co-input label="联系人手机" prop="lxrsj" v-model="data.lxrsj" placeholder="请输入联系人手机"></co-input>
      <co-input label="E-mail" prop="yzbm" v-model="data.email" placeholder="请输入E-mail"></co-input>
      <co-upload label="企业LOGO" ref="upload" :dateTxt="data.logo ? '已上传' : '未上传' "
        :serverName.sync="data.logo"></co-upload>
    </co-form>
    <div class="btn-wrap">
      <button class="weui-btn weui-btn_primary" @click.prevent="save" type="button">提交</button>
    </div>
  </div>
</template>
<script>
import coForm from "base/form/form";
import coInput from "base/form/input";
import coSelect from "base/form/select";
import { validator } from "common/js/valid";
import coLevelPicker from "base/form/levelPicker";
import coTextarea from "base/form/textarea/textarea";
import treeSelect from "base/form/treeSelect";
import treeMoresele from "base/form/treeMoresele";
import coUpload from "base/form/upload/upload";

export default {
  name: "BaseInfo",
  components: {
    coForm,
    coInput,
    coSelect,
    coLevelPicker,
    coTextarea,
    coUpload,
    treeSelect,
    treeMoresele
  },
  data() {
    return {
      math1: "1",
      math2: "2",
      math3: "3",
      qyflag: false,
      qtflag: false,
      // alertflag:
      data: {
        sffb:"",//是否发布
        dwhy:"",//单位行业
        dwlx: '', //单位类型
        dwbh: "", //单位编号
        zjhm: "", //统一社会信用码
        dwmc: "", // 单位名称
        dwjbr: "", // 单位经办人
        zcdxzqh: "", // 注册地行政区划
        zcdxzqh_mc: "", // 注册地名称
        jydxzqh: "", // 经营地行政区划
        jydxzqh_mc: "", // 经营地名称
        jydz: "", // 经营地
        dwjj: "", // 单位简介
        lxrdh: "", //联系人电话
        lxrsj: "", //联系人手机
        email: "", // 邮编
        logo: "" //企业logo
      },
      sffbList:[{ dmid: "01", dmmc: "是" },{ dmid: "02", dmmc: "否" }],//是否发布
      rules: {
        dwlx: validator('32,"full","单位类型",false'),
        dwjbr: validator('32,"full","单位经办人",false'),
        dwhy: validator('32,"full","单位行业",false'),
        qybq: validator('64,"full","企业标签",true'),
        qygm: validator('64,"full","企业规模",false'),
        qyrs: validator('8,"full","企业人数",false'),
        dwxz: validator('64,"full","单位性质",false'),
        qydj: validator('64,"full","企业登记注册类型",true'),
        qtlx: validator('64,"full","其他登记注册类型",true'),
        zcdxzqh: validator('12,"full","注册地行政区划",false'),
        jydxzqh: validator('12,"full","经营地行政区划",false'),
        jydz: validator('128,"full","经营地址",false'),
        dwjj: validator('1000,"full","单位简介",false'),
        lxrdh: validator('32,"full","固定电话",true'),
        lxrsj: validator('11,full,"联系人手机",true'),
        yzbm: validator('32,email,"E-mail",true')
      },
      codeTable: {
        zyjs_d_dwlx: [], //单位类型
        zyjs_d_qygm: [], // 企业规模
        zyjs_d_qybq: [], // 企业标签

        cdl_orgtype: [], // 单位性质
        cdg_regtype:[], // 企业登记注册类型
        cdl_orgtype_01: []// 其他登记注册类型
      }
    };
  },
  watch:{
    "data.logo": function (val) {
      this.data.logo = val&&val;
    },
    "data.dwxz": function(val){
      switch (val) {
        case '10':
          this.qyflag = true;
          this.qtflag = false;
          break;
        case '90':
          this.qtflag = true;
          this.qyflag = false;
          break;
        default:
          this.qyflag = false;
          this.qtflag = false;
          break;
      }
    },
    "data.sffb": function(val){
      this.$nextTick(_=>{
        this.data.sffb = val ? val : '01'
      })
    }
  },
  methods: {
    qygmClick(){
      switch(this.data.qygm){
        case '01':
            this.$alert('您录入的单位行业为牛的饲养，企业规模为大型，该类企业营业收入应大于等于20000万元。')
            break;
        case '02':
            this.$alert('您录入的单位行业为牛的饲养，企业规模为中型，该类企业营业收入应大于等于500万元，小于20000万元。')
            break;
        case '03':
            this.$alert('您录入的单位行业为牛的饲养，企业规模为小型，该类企业营业收入应大于等于50万元，小于500万元。')
            break;
        default:
            this.$alert('您录入的单位行业为牛的饲养，企业规模为微型，该类企业营业收入应小于50万元。')
            break;
      }
    },
    save(next) {
      this.$refs.form.validate(() => {
        if (this.data.lxrdh || this.data.lxrsj) {
          this.POST_save();
        } else {
          this.$refs.form.errorMsg = "请填写手机或固定电话！";
        }
      });
    },
    /* 保存信息修改 */
    POST_save() {
      if (this.data.dwxz == '90' && this.data.qtdjzclx == '') {
        this.$refs.form.errorMsg = "其他登记注册类型不能为空！";
        return;
      }
      this.$confirm("确定保存？", "提示")
        .then(() => {

          Object.keys(this.data).forEach(item => {
            if(this.data[item] instanceof Array){
              this.data[item] = this.data[item].toString();
            }
          });
          this.$loading = true;
          this.$http
            .post("/dwgl/dwxxBc", this.data)
            .then(res => {
              const resData = res.returnData;
              this.$alert(resData.message, () => {
                const code = +resData.executeResult;
                if (code === 1) this.$router.push({ path: "/person/user" });
              });
            })
            .finally(() => {
              this.$loading = false;
            });
        })
        .catch(() => {
          this.$toast("选择了取消");
        });
    },
    /*  查询基本信息 */
    GET_unitInfo() {
      this.$loading = true;
      this.$http
        .get("/dwgl/dwxxCx")
        .then(res => {
          const resData = res.returnData;
          this.data = resData.dwxx;
          this.data.qybq = this.data.qybq && this.data.qybq.split(',');
        })
        .finally(() => {
          this.$loading = false;
        });
    },
    /* 获取代码表 */
    GET_codeTable() {
      const arrTableName = Object.keys(this.codeTable);
      this.$http.get('/dmbgl/dmblbCx', {
        params: {
          table_name: arrTableName.join()
        }
      }).then((res) => {
        this.getCodeTabled = true;
        const data = res.returnData.dmblb;
        arrTableName.forEach(item => {
          this.codeTable[item] = data[item];
        });
      });
    },
  },
  created() {
    this.GET_codeTable();
    this.GET_unitInfo();
    // 隐藏home按钮
    this.$store.commit("SET_VISIBLE_ALL", false);
  }
};
</script>
<style scoped lang="scss">
@import "~common/scss/var.scss";
.wrap {
  background: $color-bg;
  overflow: auto;
  .btn-wrap {
    display: flex;
    .weui-btn {
      margin: 20px auto;
      flex: 0 1 60%;
      margin-top: $margin-base;
    }
  }
  .weui-btn_primary {
    background-color: #06a8f6;
  }
}
</style>
